﻿using System;
using System.Collections.Generic;
using System.Linq;
using Sustainalytics.Entities;

namespace Sustainalytics.Portfolios.Service.Models
{
    public class SubscribeToPortfolioDto
    {
        public Guid Id { get; set; }
        public string AlertName { get; set; }
        public List<PortfolioSubscribeDto> PortfolioList { get; set; }
        public Guid UserId { get; set; }
        public Guid AccountId { get; set; }
        public bool Enabled { get; set; }
        public int Frequency { get; set; }

        public static SubscribeToPortfolioDto CreateFull(SubscribeToPortfolio subscribe, ICollection<Portfolio> portfolios)
        {
            return new SubscribeToPortfolioDto()
            {
                Id = subscribe.Id,
                AccountId = subscribe.AccountId,
                UserId = subscribe.UserId,
                AlertName = "Controversy indicator change",
                Enabled = subscribe.Enabled,
                Frequency = subscribe.Frequency,
                PortfolioList = GetPortfolioSubscribeDto(portfolios, subscribe.UserId, subscribe.AccountId)
            };
        }

        public static List<PortfolioSubscribeDto> GetPortfolioSubscribeDto(ICollection<Portfolio> portfolios, Guid userId, Guid accountId)
        {
            List<PortfolioSubscribeDto> portfolioList = new List<PortfolioSubscribeDto>();
            foreach(var portfolio in portfolios)
            {
                PortfolioSubscribeDto item = new PortfolioSubscribeDto{
                    IsMyPortfolio = (portfolio.AccountIds.Contains(accountId) && portfolio.AddedByUserId == userId),
                    PortfolioId = portfolio.Id,
                    PortfolioName = portfolio.Name
                };
                portfolioList.Add(item);
            }
            return portfolioList;
        }
    }

    

    public class PortfolioSubscribeDto
    {
        public string PortfolioName { get; set; }
        public Guid PortfolioId { get; set; }
        public bool IsMyPortfolio { get; set; }
    }
}
